/*******************************************************************************
 *  Copyright (c) 2010 Chase Technology Ltd.
 *  
 *  All rights reserved. This program and the accompanying materials
 *  are made available under the terms of the GNU General Public License v3.0
 *  which is available at http://www.gnu.org/licenses/
 * 
 * Contributors:
 *     Doug Satchwell - initial API and implementation
 *******************************************************************************/
package org.jfunction.internal.parser;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;
import org.jfunction.JFunctionFile;

/**
 * Parser for the scenario sections of a scenario file
 * 
 * @author Doug Satchwell
 */
public class ExamplesParser extends AbstractParser {

	public ExamplesParser(KeyWords keywords) {
		super(new ExamplesScanner(keywords));
	}

	protected void doParse(JFunctionFile jf) throws BadLocationException {
		IToken token;
		List<String> fields = new ArrayList<String>();
		while ((token = scanner.nextToken()) != Token.EOF) {
			System.out.println(token);
			if (JFunctionToken.EXAMPLES == token) {
				// do nothing
			} else if (JFunctionToken.EXAMPLE_FIELD == token) {
				String text = getText().substring(1);
				System.out.println(text);
				fields.add(text);
			} else if (token.isWhitespace() && "\n".equals(getText())) {
				jf.addExample(fields.toArray(new String[0]));
				fields.clear();
			}
		}
		if (fields.size() > 0) {
			jf.addExample(fields.toArray(new String[0]));
		}
	}
}